﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using prufa.Models;
using prufa.ViewModels;
using prufa.Interfaces;

namespace prufa.Controllers
{
    public class GameController : Controller
    {
           private IAppRepository _repo = null;

            public GameController()
            { 
                _repo = new AppRepository();
            }

            public GameController(IAppRepository temp)
            {
                _repo = temp;
            }

        public ActionResult Index()
        {
            var games = from game in _repo.AllGames 
                        select game; 
            return View(games);
        }
        
        /*returns a View with a viewmodel which consist of lists of joinable
        Mylla games and active mylla games that we get from the active games repository-HÖÞ*/
        [HttpGet]
        public ActionResult Mylla()
        {        
            ActiveGamesRepository ag = new ActiveGamesRepository();
            IEnumerable<Mylla>joinable = ag.GetJoinableGames();
            IEnumerable<Mylla> allactive = ag.AllActiveGames();
            return View(new MyllaGames { AllActiveGames = allactive, JoinableGames = joinable });
        }

        //when we create an "instance" of mylla then go to this function-HÖÞ
        [HttpPost]
        public ActionResult Mylla(string InstanceName)
        {
            DefaultConnection db = new DefaultConnection();

            //myUser is a way to find the user who created the game"instance" -HÖÞ
            UserProfile myUser = (from n in  db.UserProfiles
                         where n.UserName == User.Identity.Name
                          select n).First();

            //myGame returns the right game(mylla in this case)-HÖÞ
            Game myGame = (from n in   db.Games
                         where n.GameId == 1
                          select n).First();
            //add one to the play counter for my game -HÖÞ
             myGame.PlayCounter += 1;

             //save the changes in the db -HÖÞ
             db.SaveChanges();

             /*if the string passed from the view is null or just whitespace then a game is not created
              and the user is just redirectid to the same view he was in -HÖÞ*/
             if (string.IsNullOrWhiteSpace(InstanceName))
             {
                 return RedirectToAction("Mylla","Game");
             }

             //create a new instance of mylla and then fill in the information accordingly -HÖÞ
            Mylla nytt = new Mylla();
            nytt.InstanceName = InstanceName;
            nytt.GameID = myGame;
            nytt.IsJoinable = true;
            nytt.Player1 = myUser;
            nytt.Player2 = null;

            //add this new Mylla to the Myllas database and save the changes -HÖÞ
            db.Myllas.Add(nytt);
            db.SaveChanges();

            //redirect the user to the new game "instance" -HÖÞ
            return RedirectToAction("ActiveMylla", new { id = nytt.MyllaId});
        }

        //restricted access so that only those who are signed in can go here -HÖÞ
        [Authorize]
        public ActionResult ActiveMylla(int id)
        {
            DefaultConnection db = new DefaultConnection();
         //find the right game from the id passed from the view
            var game = db.Myllas.Find(id);

            //find the user
            UserProfile myUser = (from n in db.UserProfiles
                                  where n.UserName == User.Identity.Name
                                  select n).First();
            GameRepository gr = new GameRepository();

            //if the player
            if (game.Player1.UserName != User.Identity.Name)
            {
                game.Player2 = myUser;
                game.IsJoinable = false;

                gr.AddToPlayCountMylla();
                db.SaveChanges();

            }
            db.SaveChanges();
            return View(game);
        }

        [HttpGet]
        public ActionResult Risk()
        {          
            ActiveGamesRepository ag = new ActiveGamesRepository();
            IEnumerable<Risk> joinable = ag.GetJoinableRiskGames();
            IEnumerable<Risk> allactive = ag.AllActiveRiskGames();
            return View(new RiskGames {Allactive = allactive, Joinable = joinable });
        }

        [HttpPost]
        public ActionResult Risk(string InstanceName)
        {
            DefaultConnection db = new DefaultConnection(); 

            UserProfile myUser = (from n in db.UserProfiles 
                                  where n.UserName == User.Identity.Name
                                  select n).First();
            Game myGame = (from n in db.Games 
                           where n.GameId == 2
                           select n).First();
            myGame.PlayCounter += 1;
            db.SaveChanges();
            if (string.IsNullOrEmpty(InstanceName))
            {
                return RedirectToAction("Risk", "Game");
            }
            Risk nytt = new Risk();
            nytt.InstanceName = InstanceName;
            nytt.GameID = myGame;
            nytt.IsJoinable = true;
            nytt.Player1 = myUser;
            nytt.Player2 = null;           
            db.Risks.Add(nytt);
            db.SaveChanges();           
            var games = db.Myllas.ToList();
            return RedirectToAction("ActiveRisk", new { id = nytt.RiskId });
        }


      
        [Authorize]
        public ActionResult ActiveRisk(int id)
        {
            DefaultConnection db = new DefaultConnection();

            var game = db.Risks.Find(id);

            UserProfile myUser = (from n in db.UserProfiles
                                  where n.UserName == User.Identity.Name
                                  select n).First();
            GameRepository gr = new GameRepository();

            if (game.Player1.UserName != User.Identity.Name)
            {
                game.Player2 = myUser;
                game.IsJoinable = false;
                gr.AddToPlayCountRisk();
                
                db.SaveChanges();

            }
            db.SaveChanges();
            return View(game);
        }
    }
}
